from pydantic import BaseModel, Field
from typing import Optional, Dict, List, Any
from datetime import datetime

class UserBase(BaseModel):
    username: str
    email: str

class UserCreate(UserBase):
    password: str

class User(UserBase):
    id: int

    class Config:
        orm_mode = True

class ItemBase(BaseModel):
    title: str
    description: Optional[str] = None

class ItemCreate(ItemBase):
    pass

class Item(ItemBase):
    id: int
    owner_id: int

    class Config:
        orm_mode = True

class AudioProcessingRequest(BaseModel):
    """音频处理请求模型"""
    min_note_duration: float = Field(default=0.05, description="最小音符持续时间（秒）")
    tempo_bpm: int = Field(default=120, description="速度（每分钟节拍数）")

class AudioMetadata(BaseModel):
    """音频元数据模型"""
    duration: float = Field(..., description="音频时长（秒）")
    sample_rate: int = Field(..., description="采样率")
    channels: int = Field(..., description="声道数")
    format: str = Field(..., description="音频格式")
    file_size: int = Field(..., description="文件大小（字节）")

class NoteInfo(BaseModel):
    """音符信息模型"""
    pitch: int = Field(..., description="MIDI音高")
    start_time: float = Field(..., description="开始时间（秒）")
    duration: float = Field(..., description="持续时间（秒）")
    velocity: int = Field(..., description="力度")
    note_name: str = Field(..., description="音符名称（如C4）")

class ChordInfo(BaseModel):
    """和弦信息模型"""
    root: str = Field(..., description="根音")
    quality: str = Field(..., description="和弦性质（如major, minor）")
    start_time: float = Field(..., description="开始时间（秒）")
    duration: float = Field(..., description="持续时间（秒）")

class MusicInfo(BaseModel):
    """音乐信息模型"""
    tempo: int = Field(..., description="速度（每分钟节拍数）")
    notes: List[Dict[str, Any]] = Field(..., description="音符列表")

class ProcessingResult(BaseModel):
    """处理结果模型"""
    status: str = Field(..., description="处理状态")
    metadata: AudioMetadata = Field(..., description="音频元数据")
    music_info: MusicInfo = Field(..., description="音乐信息")
    output_files: Dict[str, str] = Field(..., description="输出文件路径")
    processing_time: float = Field(..., description="处理时间（秒）")
    created_at: datetime = Field(default_factory=datetime.now, description="创建时间")

class ErrorResponse(BaseModel):
    """错误响应模型"""
    status: str = Field("error", description="状态")
    error_code: int = Field(..., description="错误代码")
    message: str = Field(..., description="错误信息")
    details: Optional[Dict] = Field(None, description="详细信息")
